package com.example.back.service.impl;

import com.example.back.constants.TimeUnit;
import com.example.back.dto.resp.CheckupProductList;
import com.example.back.mapper.CheckupProductMapper;
import com.example.back.service.CheckupProductService;
import com.example.common.entity.CheckupProduct;
import com.example.common.support.ApiException;
import com.example.common.utils.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * @ClassName CheckupProductServiceImpl
 * @Author wengjt
 * @Date 2021/5/8 11:16
 * @Description
 **/
@Service
public class CheckupProductServiceImpl implements CheckupProductService {
    @Resource
    private CheckupProductMapper checkupProductMapper;

    /**
     * 多条件查询体检套餐列表
     *
     * @Author wengjt
     * @Date 11:18 2021/5/8
     */
    @Override
    public CheckupProductList getCheckupProductList(String cpName, Long ctId, Integer showAble, Integer pageNum, Integer pageSize) {
        CheckupProductList result = new CheckupProductList();
        PageInfo<CheckupProduct> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            checkupProductMapper.multipleSearch(cpName, ctId, showAble);
        });
        result.setList(pageInfo.getList());
        result.setCount(pageInfo.getTotal());
        return result;
    }

    /**
     * 获取单个体验套餐详细
     *
     * @Author wengjt
     * @Date 11:20 2021/5/8
     */
    @Override
    public CheckupProduct getInfo(Long cpId) {

        return checkupProductMapper.findOneById(cpId);
    }

    /**
     * 修改体检套餐
     *
     * @Author wengjt
     * @Date 11:36 2021/5/8
     */
    @Override
    public Object UpdateCheckupProduct(CheckupProduct checkupProduct) {
        CheckupProduct oneById = checkupProductMapper.findOneById(checkupProduct.getCpId());
        oneById.setName(checkupProduct.getName());
        oneById.setDisable(checkupProduct.getDisable());
        oneById.setCtId(checkupProduct.getCtId());
        oneById.setItemAmount(checkupProduct.getItemAmount());
        oneById.setItemContent(checkupProduct.getItemContent());
        oneById.setNotice(checkupProduct.getNotice());
        oneById.setPrecautions(checkupProduct.getPrecautions());
        oneById.setLabel(checkupProduct.getLabel());
        oneById.setGender(checkupProduct.getGender());
        oneById.setRecommend(checkupProduct.getRecommend());
        long addedEffectiveTime = 0;
        //计算上架有效时间
        switch (checkupProduct.getAddedEffectiveTimeUnit()) {
            case 1:
                //小时
                addedEffectiveTime = checkupProduct.getAddedEffectiveTimeAmount() * 60;
                break;
            case 2:
                //天
                addedEffectiveTime = checkupProduct.getAddedEffectiveTimeAmount() * 60 * 24;
                break;
            case 3:
                //周
                addedEffectiveTime = checkupProduct.getAddedEffectiveTimeAmount() * 60 * 24 * 7;
                break;
            case 4:
                //月
                addedEffectiveTime = checkupProduct.getAddedEffectiveTimeAmount() * 60 * 24 * 30;
                break;
            case 5:
                //年
                addedEffectiveTime = checkupProduct.getAddedEffectiveTimeAmount() * 60 * 24 * 365;
                break;
        }
        oneById.setAddedEffectiveTime(addedEffectiveTime);
        oneById.setIcon(checkupProduct.getIcon());
        checkupProductMapper.save(oneById);
        return null;
    }

    /**
     * 新增体验套餐
     *
     * @Author wengjt
     * @Date 11:36 2021/5/8
     */
    @Override
    public Object insertCheckProduct(CheckupProduct checkupProduct) {
        if(checkupProduct.getShowAble()==1){
            checkupProduct.setAddedTime(new Date());
        }
        long addedEffectiveTime = 0;
        //计算上架有效时间
        switch (checkupProduct.getAddedEffectiveTimeUnit()) {
            case 1:
                //小时
                addedEffectiveTime = checkupProduct.getAddedEffectiveTimeAmount() * 60;
                break;
            case 2:
                //天
                addedEffectiveTime = checkupProduct.getAddedEffectiveTimeAmount() * 60 * 24;
                break;
            case 3:
                //周
                addedEffectiveTime = checkupProduct.getAddedEffectiveTimeAmount() * 60 * 24 * 7;
                break;
            case 4:
                //月
                addedEffectiveTime = checkupProduct.getAddedEffectiveTimeAmount() * 60 * 24 * 30;
                break;
            case 5:
                //年
                addedEffectiveTime = checkupProduct.getAddedEffectiveTimeAmount() * 60 * 24 * 365;
                break;
        }
        checkupProduct.setAddedEffectiveTime(addedEffectiveTime);
        checkupProduct.setDisable(0);
        checkupProduct.setCreateTime(new Date());
        checkupProductMapper.insertOne(checkupProduct);
        return null;
    }

    /**
     * 上下架
     *
     * @Author wengjt
     * @Date 11:36 2021/5/8
     */
    @Override
    public Object putawayCheckupProduct(Long cpId, Integer showAble) {
        CheckupProduct oneById = checkupProductMapper.findOneById(cpId);
        if (showAble.equals(1)) {
            //上架设置上架时间
            oneById.setAddedTime(new Date());
        }
        oneById.setShowAble(showAble);
        checkupProductMapper.save(oneById);
        return null;
    }

    /**
     * 删除体检套餐
     *
     * @Author wengjt
     * @Date 11:37 2021/5/8
     */
    @Override
    public Object deleteCheckupProduct(Long cpId) {
        CheckupProduct oneById = checkupProductMapper.findOneById(cpId);
        if (oneById.getShowAble() == 1) {
            throw new ApiException(400, "已经上架的套餐不允许删除");
        }
        oneById.setDisable(1);
        checkupProductMapper.save(oneById);
        return null;
    }
}
